#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "./../../inc/login.h"

user_Node_p user_headNode = NULL; // 用户头指针
int user_index            = -1;   // 登录用户索引
user_Node_p user_currNode = NULL; // 当前用户指针

user_Node_p user_create()
{
    user_Node_p currNode = (user_Node_p)malloc(sizeof(user_Node_t));
    currNode->data       = (user_p)malloc(sizeof(user_t));
    currNode->next       = currNode;
    currNode->prev       = currNode;
    return currNode;
}

user_Node_p find_userNode(user_Node_p headNode, char * userID)
{
    user_Node_p currNode = headNode->next;
    while(currNode != headNode) {
        if(strcmp(currNode->data->userID, userID) == 0) {
            return currNode;
        } else {
            user_index++;
            currNode = currNode->next;
        }
    }
    return NULL;
}

void save_file_user()
{
    FILE * fp;
    fp = fopen("./res/user.bin", "wb");
    if(fp == NULL) {
        perror("open file error");
        return;
    }

    // 写入文件标识符
    const char magic[] = "USER_BIN";
    fwrite(magic, sizeof(char), strlen(magic), fp);

    user_Node_p p = user_headNode->next;

    while(p != user_headNode && p->data != NULL) {
        // 直接写入整个数据结构
        size_t written = fwrite(p->data, sizeof(user_t), 1, fp);
        if(written != 1) {
            perror("写入数据失败");
            break;
        }
        p = p->next;
    }

    fclose(fp);
}

user_Node_p load_file_user()
{
    FILE * fp = fopen("./res/user.bin", "rb"); // 二进制读模式
    if(fp == NULL) {
        perror("open file error");
        return NULL;
    }

    // 检查文件标识符
    char magic[10];
    if(fread(magic, sizeof(char), 8, fp) != 8 || strncmp(magic, "USER_BIN", 8) != 0) {
        printf("错误：不是有效的二进制文件格式\n");
        fclose(fp);
        return NULL;
    }
    magic[9] = '\0';

    user_Node_p headNode = user_create();
    if(!headNode) {
        fclose(fp);
        return NULL;
    }

    // 初始化空链表
    headNode->next   = headNode;
    headNode->prev   = headNode;
    user_Node_p tail = headNode;

    while(1) {
        user_Node_p new_node = (user_Node_p)malloc(sizeof(user_Node_t));
        user_p data          = (user_p)malloc(sizeof(user_t));
        if(new_node == NULL || data == NULL) {
            perror("malloc error");
            break;
        }

        // 读取整个数据结构
        size_t read = fread(data, sizeof(user_t), 1, fp);
        if(read != 1) {
            // 可能到达文件末尾或读取错误
            free(data);
            free(new_node);
            break;
        }

        // 添加到链表
        new_node->data = data;
        new_node->next = headNode;
        new_node->prev = tail;

        tail->next     = new_node;
        headNode->prev = new_node;
        tail           = new_node;
    }

    fclose(fp);
    return headNode;
}

// 登录 返回 0 表示成功 -1 表示密码错误 -2 表示用户不存在
int login(user_Node_p headNode, char * userID, char * password)
{
    user_Node_p currNode = malloc(sizeof(user_Node_t));
    currNode             = find_userNode(headNode, userID);
    if(currNode != NULL) {
        if(strcmp(currNode->data->password, password) == 0) {
            user_currNode = currNode;
            return 0;
        } else {
            return -1;
        }
    } else {
        return -2;
    }
}

// 注册 返回 0 表示成功 -1 表示用户已存在 -2 内存分配错误
int register_user(user_Node_p headNode, char * userID, char * password)
{
    // 检查用户是否已存在
    if(find_userNode(headNode, userID) != NULL) {
        printf("用户 %s 已存在，注册失败\n", userID);
        return -1;
    }

    // 创建新节点
    user_Node_p newNode = (user_Node_p)malloc(sizeof(user_Node_t));
    if(!newNode) {
        return -2;
    }

    // 分配用户数据内存
    newNode->data = (user_t *)malloc(sizeof(user_t));
    if(!newNode->data) {
        printf("内存分配失败\n");
        free(newNode);
        return -2;
    }

    // 初始化用户数据
    memset(newNode->data, 0, sizeof(user_t));
    strncpy(newNode->data->userID, userID, sizeof(newNode->data->userID) - 1);
    strncpy(newNode->data->password, password, sizeof(newNode->data->password) - 1);

    if(headNode->prev != NULL) {
        // 添加到链表尾部
        newNode->prev        = headNode->prev;
        newNode->next        = headNode;
        headNode->prev->next = newNode;
        headNode->prev       = newNode;
    } else {
        // 第一个用户节点
        newNode->prev  = headNode;
        newNode->next  = headNode;
        headNode->next = newNode;
        headNode->prev = newNode;
    }

    printf("用户 %s 注册成功\n", userID);

    // 6. 保存到文件
    save_file_user(headNode);
    return 0;
}